package lc20240502;

import java.util.HashMap;

//205 同构字符串
public class Demo2 {
    public static void main(String[] args) {
        String s = "badc";
        String t = "baba";
        System.out.println("isIsomorphic(s,t) = " + isIsomorphic(s, t));
    }

    public static boolean isIsomorphic(String s, String t) {
        if (s.length() != t.length())
            return false;
        HashMap<Character, Character> map = new HashMap<>();

        for (int i = 0; i < s.length(); i++) {

            if (map.containsKey(s.charAt(i))) {
                if (map.get(s.charAt(i))!=t.charAt(i)){
                    return false;
                }
            }//不能出现多对一的情况。采用containsValue方法判断是否含有重复值。
            else if (map.containsValue(t.charAt(i))){
                return false;
            }
            map.put(s.charAt(i),t.charAt(i));
        }
        return true;
    }
}
